#!/usr/bin/env bash
#
# Copyright 2018, Intel Corporation
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#
#     * Neither the name of the copyright holder nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# daxio/TEST1 -- test for daxio utility; negative scenarios
#

. ../unittest/unittest.sh

require_test_type medium
require_fs_type any
require_dax_devices 1

setup

# must be done after setup, when daxio path is already known
require_binary $DAXIO$EXESUFFIX

LOG=out$UNITTEST_NUM.log

# invalid offset/length
expect_abnormal_exit $DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -k 0x1234abzz &>$LOG
expect_abnormal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -s foo &>>$LOG
expect_abnormal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -l 20xxx &>>$LOG

# zero flag w/o output
expect_abnormal_exit $DAXIO$EXESUFFIX -z &>>$LOG

# no input/output
expect_abnormal_exit $DAXIO$EXESUFFIX -l 100 &>>$LOG

# offset w/o input or output
expect_abnormal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -k 100 &>>$LOG
expect_abnormal_exit $DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -s 100 &>>$LOG

# neither input or output is device dax
expect_abnormal_exit $DAXIO$EXESUFFIX -i /dev/zero -o $DIR/dummy -l 16384 &>>$LOG

# requested size larger than source - no error, but will copy less than requested
create_holey_file 4KB $DIR/dummy
expect_normal_exit $DAXIO$EXESUFFIX -i $DIR/dummy -o ${DEVICE_DAX_PATH[0]} -l 8192 &>>$LOG

# offset/length beyond device size
DEVSIZE=`$PMEMDETECT -z ${DEVICE_DAX_PATH[0]}`

expect_abnormal_exit $DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o /dev/null -k $(($DEVSIZE + 100)) &>>$LOG
expect_abnormal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -i /dev/zero -s $(($DEVSIZE + 1)) &>>$LOG

# these succeed, but copy less bytes than requested
expect_normal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -i /dev/zero -l $(($DEVSIZE + 1000)) &>>$LOG
expect_normal_exit $DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o /dev/null -k $(($DEVSIZE / 2)) -l $DEVSIZE &>>$LOG
expect_normal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -i /dev/zero -s $(($DEVSIZE - 100)) -l $DEVSIZE &>>$LOG
expect_normal_exit $DAXIO$EXESUFFIX -i ${DEVICE_DAX_PATH[0]} -o $DIR/dummy_out -k $(($DEVSIZE - 100)) -l $DEVSIZE &>>$LOG
expect_normal_exit $DAXIO$EXESUFFIX -o ${DEVICE_DAX_PATH[0]} -i $DIR/dummy -s $(($DEVSIZE - 100)) -l $DEVSIZE &>>$LOG

check

pass
